package com.ratta.supernote.note.plugincore.view.touchview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.events.RCTEventEmitter;

public class TouchView extends ViewGroup {

    private final String TAG = "TouchView";

    /// Forbidden input type, -1 means any touch input type
    private int forbidTouchType = -1;
    ReactContext mReactContext;

    public TouchView(Context context) {
        this(context, null);
    }

    public TouchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TouchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public TouchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        if (context instanceof ReactContext) {
            mReactContext = (ReactContext) context;
        }
        setBackgroundColor(Color.TRANSPARENT);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }


    Point lastDownPoint = null;
    long lastDownTime = 0;

    @SuppressLint("WrongConstant")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG,"onTouchEvent test y:"+event.getY());
                lastDownPoint = new Point((int) event.getX(), (int) event.getY());
                lastDownTime = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                Point upPoint = new Point((int) event.getX(), (int) event.getY());
                boolean isClick = isClick(lastDownPoint, upPoint);
                Log.i(TAG,"onTouchEvent up type:"+event.getToolType(0)+forbidTouchType);
                if(isClick && event.getToolType(0) != forbidTouchType) {
                    sendClickEvent2RN(event);

                }
                lastDownTime = 0;
                break;
        }

        return true;
    }

    private boolean isClick(Point downPoint, Point upPoint) {
        long currentTime = System.currentTimeMillis();
        Log.i(TAG,"isClick time:"+(currentTime - lastDownTime));
        if(currentTime - lastDownTime >=500) {
            return false;
        }
        Log.i(TAG,"isClick downPoint:"+downPoint.toString()+"=="+upPoint.toString());
        if(Math.abs(downPoint.x - upPoint.x) <=20 && Math.abs(downPoint.y - upPoint.y) <=20) {
            return  true;
        }
        return false;


    }

    private void sendClickEvent2RN(MotionEvent event) {
        WritableMap params = Arguments.createMap();
        params.putDouble("x", event.getX());
        params.putDouble("y", event.getY());
        mReactContext.getJSModule(RCTEventEmitter.class)
                .receiveEvent(getId(), "onClick", params);
    }


    private void sendInputType2RN(int touchType) {
        WritableMap params = Arguments.createMap();
        params.putInt("touchType", touchType);
        mReactContext.getJSModule(RCTEventEmitter.class)
                .receiveEvent(getId(), "onTouchType", params);

    }

    public void setForbidTouchType(int forbidTouchType) {
        this.forbidTouchType = forbidTouchType;
    }
}
